var fs = require('fs')
var rp = require('request-promise')
var cheerio = require('cheerio')
var tough = require('tough-cookie');

var config = require('./config.js')
var util = require('./util/_default.js')
var xlsx = require('node-xlsx')

var proxyUrl = '', trycount = 10
var page, maxpage, ip

var iplist = ['49.87.75.141:37420', '115.215.51.246:44451', '114.239.250.246:27848']

var helper = util.helper

var dt = [['序号', '产品编码', '标书号', '分类', '产品名称', '规格',
    '型号', '品牌', '计量单位', '包装规格', '投标企业名称', '生产厂家', '质量层次', '中标价', '中标联动价', '地州']]

var href = 'http://182.92.8.25:8115/Module/ComBidderPrice/BidResult/BidLook.aspx'

function main() {

    ip = '60.167.21.59:23098'
    var opts = helper.deepCopy(config.reqOpts)

    let cookie = new tough.Cookie({
        key: "ASP.NET_SessionId",
        value: "dkfkjd45udxxzrv0eqa25fu1",
        domain: '182.92.8.25',
        httpOnly: true,
        maxAge: 31536000
    });

    var cookiejar = rp.jar();
    cookiejar.setCookie(cookie, 'http://182.92.8.25');

    opts.url = href
    opts.method = 'get'
    opts.headers.Referer = href
    opts.jar = cookiejar

    opts.proxy = 'http://' + ip

    rp(opts).then(function (body) {
        var $ = cheerio.load(body)
        var data = helper.getFormData($('form#form1').serializeArray())
        page = 1
        GetDetail(data, cookiejar, false);
    }).catch(function (err) {
        console.log(err)
        setTimeout(function () {
            main()
        }, Math.random() * 10 * 1000)
    })
}

function GetDetail(data, cookiejar, isnext) {
    console.log('正在抓取第' + page + '页,当前总数：' + dt.length)
    var opts = helper.deepCopy(config.reqOpts)
    opts.url = href
    opts.method = 'post'
    opts.headers.Referer = href
    opts.jar = cookiejar
    //console.log(data)
    if (isnext) {
        data['__EVENTTARGET'] = 'lbtnnextpage'
    }
    data['txtProductName'] = '%%%%'
    data['txtBidderOrgName'] = '%%%%'
    data['btSelect'] = '查询'
    //data['__VIEWSTATEGENERATOR'] = 'FEC5FE02'
    opts.formData = data

    opts.proxy = 'http://' + ip

    rp(opts).then(function (body) {
        var $ = cheerio.load(body)
        var table = $('table', 'div#zhezhao')
        var tr = $('tr', table.eq(1))
        if (tr.length > 0) {
            tr.slice(1, tr.length - 1).each(function () {
                var row = []
                row.push(
                    iGetInnerText($("td", this).eq(0).text()),
                    iGetInnerText($("td", this).eq(1).text()),
                    iGetInnerText($("td", this).eq(2).text()),
                    iGetInnerText($("td", this).eq(3).text()),
                    iGetInnerText($("td", this).eq(4).text()),
                    iGetInnerText($("td", this).eq(5).attr('title')),
                    iGetInnerText($("td", this).eq(6).attr('title')),
                    iGetInnerText($("td", this).eq(7).text()),
                    iGetInnerText($("td", this).eq(8).text()),
                    iGetInnerText($("td", this).eq(9).text()),
                    iGetInnerText($("td", this).eq(10).text()),
                    iGetInnerText($("td", this).eq(11).text()),
                    iGetInnerText($("td", this).eq(12).text()),
                    iGetInnerText($("td", this).eq(13).find('input').val()),
                    iGetInnerText($("td", this).eq(14).text()),
                    iGetInnerText($("td", this).eq(15).text())
                )
                dt.push(row)
            });
            if ((dt.length - 1) % 300 == 0) {
                saveExcel(dt, page)
            }
            var next = $('#lbtnnextpage').attr('disabled')
            if (typeof (next) == "undefined") {
                page = page + 1
                GetDetail(data, cookiejar, true);
            }
            else {
                saveExcel(dt, page)
            }
        }
    }).catch(function (err) {
        console.log(err)
        setTimeout(function () {
            main()
        }, Math.random() * 10 * 1000)
    })

}

function iGetInnerText(testStr) {
    //console.log()
    if (testStr == null) {
        return ''
    } else {
        var resultStr = testStr.replace(/\ +/g, ""); //去掉空格
        resultStr = resultStr.replace(/[ ]/g, "");    //去掉空格
        resultStr = resultStr.replace(/[\t\r\n]/g, ""); //去掉回车换行
        resultStr = resultStr.replace(/[ ]/g, "");    //去掉空格
        return resultStr;
    }
}

function saveExcel(data, page) {
    d = (new Date()).Format('hhmmss')
    var file = xlsx.build([{ name: "mySheetName", data: data }])
    fs.writeFileSync('result_' + page + '_' + d + '.xlsx', file, 'binary')
}


main()



